<template>
	<el-dialog v-model="dialogVisible" title="专家评价" width="40%" :lock-scroll="false">
		<div style="margin-top: 20px">
			<HyForm :hyFormDefault="hyFormDefault" :hyFormConfig="hyFormConfig" :hyFormFields="hyFormFields" ref="hyFormRef" />
		</div>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="dialogVisible = false">取消</el-button>
				<el-button type="primary" :loading="loading" @click="submit(hyFormRef)">确认</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script lang="ts" setup>
import HyForm from '/@/components/HyForm/index.vue';
import { useDict } from '/@/hooks/dict';
import { doEvaluate } from '/@/api/expertDatabase/evaluate';
const { yes_no_type } = useDict('yes_no_type');
const { expert_score } = useDict('expert_score');
const expertAttends = computed(() => {
	let data = expert_score.value.sort(function (a: any, b: any) {
		return b.sortOrder - a.sortOrder;
	});
	return data;
});
const props = defineProps({
	listId: {
		type: String,
		default: () => {
			return '';
		},
	},
});
const router = useRouter();
//确认loading
const loading = ref(false);
const dialogVisible = ref(false);
const hyFormRef: any = ref(null);
watch(
	() => dialogVisible.value,
	(val) => {
		if (!val) {
			hyFormRef.value.resetFields();
		}
	}
);
const submit = (formEl: any) => {
	if (!formEl) return;
	formEl.validate((valid: any, fields: any) => {
		if (valid) {
			let parms = {
				id: props.listId,
				...hyFormRef.value.hyForm,
			};
			loading.value = true;
			doEvaluate(parms)
				.then((res) => {
					loading.value = false;
					if (res.code == 0) {
						dialogVisible.value = false;
						router.push({
							path: '/expertDatabase/evaluate/expert/index',
						});
					}
				})
				.catch(() => {
					loading.value = false;
				});
		}
	});
};
//默认表单值
const hyFormDefault = ref({
	attend: '',
	evaluatePunctuality: '',
	evaluateError: '',
	evaluateMark: '',
	evaluatePresentationDetail: '',
});
//配置基本信息表单
const hyFormFields = computed(() => {
	return {
		evaluateAttend: {
			label: '是否参会',
			type: 'select',
			required: true,
			options: yes_no_type.value,
		},
		evaluatePunctuality: {
			label: '是否按时参会',
			type: 'select',
			required: true,
			options: yes_no_type.value,
		},
		evaluateError: {
			label: '是否违规',
			type: 'select',
			required: true,
			options: yes_no_type.value,
		},
		evaluateMark: {
			label: '专家评分',
			type: 'radio',
			required: true,
			options: expertAttends.value,
		},
		evaluatePresentationDetail: {
			label: '专家履责详情',
			type: 'textarea',
			required: true,
		},
	};
});
//动态表单配置
const hyFormConfig = computed(() => {
	return {
		labelWidth: '120px',
		span: 24,
		offset: 0,
		type: 'submit',
	};
});
defineExpose({ dialogVisible });
</script>

<style lang="scss" scoped></style>
